<?php

namespace app\api\model;
use think\Model;
use think\Config;

class News extends Model
{
    protected $table = 'jk_news';

    //格式化时间
    public function getCreateAtAttr($value)
    {
        $res = friendlyDate($value);

        return $res;
    }

    //将帖子详情里所有的图片全部拼接域名
    public function getTextAttr($value)
    {
        $pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg]))[\'|\"].*?[\/]?>/"; 
        preg_match_all($pattern,$value,$match); 
        $text = str_replace("src=\"", Config::get('web.pic_url')['wang'], $value);

        return $text;
    }

    //所属模块
    public function getModularAttr($value)
    {
        $status = [
            '1'=>'平台担保',
            '2'=>'分享交流', 
        ];

        return $status[$value];
    }

    //审核状态
    public function getStatusAttr($value)
    {
        $status = [
            '1'=>'审核通过',
            '2'=>'待审核', 
            '3'=>'审核不通过'
        ];

        return $status[$value];
    }

     //通过user_id 找出用户的头像
    public function getUserIconUrlAttr($value)
    {
    	// $res = \think\Db::table('jk_user_informations')->where('user_id', $value)->field('praise, commonly, negative_comment')->find();
    	$res = \think\Db::table('jk_users')->alias('u')
    		->where('u.user_id', $value)
    		->join('jk_user_informations m', 'm.user_id = u.user_id', 'LEFT')
    		->join('jk_user_third t', 't.openid = u.wechat_openid', 'LEFT')
    		->field('m.user_icon_url, t.avatar')
    		->find();
    	if(!empty($res['avatar'])){
    		$info = $res['avatar'];
    	} elseif(!empty($res['user_icon_url'])){
    		$info = Config::get('web.pic_url')['user'] . $res['user_icon_url'];
    	} else{
    		$info = Config::get('web.pic_url')['default'];
    	}

        return $info;
        // return $res;
    }

}
